package com.colortea.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.colortea.dao.ShippingAddressTDao;
import com.colortea.entity.ShippingAddressT;


@Repository("shippingAddressTDao")
public class ShippingAddressTDaoImpl extends BaseTDaoImpl<ShippingAddressT> implements ShippingAddressTDao {


	private static final Logger log = LoggerFactory.getLogger(ShippingAddressTDaoImpl.class);

	@SuppressWarnings("unchecked")
	public ShippingAddressT findShippingAddressByOrderid(final String orderid, final String state) {
		log.debug("find all findShippingAddressByOrderid by shippingaddressid");
		try {
			String queryString = "from ShippingAddressT as st where st.orderid=:orderid and st.state=:state order by createtime desc";
			Query query = this.getSessionFactory().openSession().createQuery(queryString);
			query.setParameter("orderid", orderid);
			query.setParameter("state", state);
			List list = query.list();
			if (list.size() > 0) {
				return (ShippingAddressT) list.get(0);
			}
			return null;
		} catch (RuntimeException re) {
			log.error("find all findShippingAddressByOrderid by shippingaddressid error", re);
			throw re;
		}
	}
}